<?php

	// +----------------------------------------------------------------------
	// | 钩子验证器
	// +----------------------------------------------------------------------
	// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
	// +----------------------------------------------------------------------
	// | Author: 微尘 <yicmf@qq.com>
	// +----------------------------------------------------------------------

	namespace app\validate\ucenter;

	use app\common\validate\Base;
	use app\model\ucenter\Sms as SmsModel;
	use app\model\ucenter\User as UserModel;
	use think\captcha\Captcha;
	use think\Exception;
	use think\facade\Config;

	class Sms extends Base
	{
		// 当日发送数量限制
		protected static $day_limit = 0;
		// 两次发送间隔
		protected static $send_between = 0;
		// ip每天发生次数
		protected static $ip_max = 10;

		// ip白名单
		protected static $ip_white_list = ['127.0.0.1'];
		//ip 黑名单
		protected static $ip_black_list = [];
		protected static $open_token = false;


		protected $rule = [
			'mobile|手机号' => [
				'require',
//				'mobile',
				'checkMobile'
			],
			'type|发送场景' => [
				'require',
				'in:forget,register,safe_code,bind,login,forgetPwd,resetpwd,third_bind,cashPwd'
			],

//            'register' => 'SMS_180775224',
//            'resetpwd' => 'SMS_180775223',
//            'forgetPwd' => 'SMS_180775223',
//            'login' => 'SMS_180775226',
//            'third_bind' => 'SMS_180775227',
//            'cashPwd' => 'SMS_204875063',
		];
		protected $message = [
			'mobile.token' => '页面超时，请刷新后再试',
			'type.in' => '发送方式异常',
		];

		/**
		 * 发送手机安全验证
		 * @param $value
		 * @param $rule
		 * @param $data
		 * @return string
		 * @author 微尘 <yicmf@qq.com>
		 * @datetime: 2020/6/23 8:34
		 */
		protected function checkMobile($value, $rule, $data)
		{
			if (self::$open_token && false == $this->token($data['__token__'], '__token__', $data)) {
				return '页面超时，请刷新后再试';
			}
			// 检验当日发送次数
			if (self::$day_limit > 0 && SmsModel::where('mobile', $value)->whereTime('create_time', 'today')->where('status', 1)->count('*') > self::$day_limit) {
				return '当天短信频率超过上限';
			}
			// 检查上次发送
			if (self::$send_between > 0 && SmsModel::where('mobile', $value)->whereTime('create_time', '>', time() - self::$send_between)->where('status', 1)->count('*') > 0) {
				return '请60秒后再试';
			}

//			if (in_array($data['type'],['register','forget','login']) && !captcha_check($data['captcha'])) {
//				// 验证失败
//				return '验证码填写错误';
//			}
			// 需要填写验证码
			if ('bind' == $data['type'] || 'safe_code'==$data['type']) {
				if (!$this->request->user) {
					return '请登录后再试';
				}
			}
//			elseif (!captcha_check($data['captcha'])) {
//				return '验证码填写错误';
//			}

			if ('bind' == $data['type'] || 'register' == $data['type']) {
				// 需要无人使用
				if (UserModel::where('mobile', $value)->where('status', '>', -2)->find()) {
					return '当前手机号已经注册';
				}
			} elseif ('forget' == $data['type'] || 'forgetPwd' == $data['type'] || 'resetpwd' == $data['type'])  {
				// 忘记密码
				$user = UserModel::where('mobile', $value)->where('status', '>', -2)->find();
				if (!$user) {
					return '当前账户未注册';
				}
			}
			// 短信黑白名单
			if (in_array($this->request->ip(), self::$ip_black_list)) {
				return '当前IP已禁用短信';
			}
			if (in_array($this->request->ip(), self::$ip_white_list)) {
				return true;
			}
			// 检查ip发送频率
			if (self::$ip_max > 0 && SmsModel::where('ip', $this->request->ip())->where('mobile', $value)->whereTime('create_time', 'd')->count('*') > 0) {
				return '当日IP达到发送上限';
			}
			return true;
		}

	}
